package com.example.Application;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

@Slf4j
public class FactoryApplicationContext {

    /**
     * @param annotationConfig JavaConfig.Class
     * @return ApplicationContext
     */
    public static AnnotationConfigApplicationContext getApplicationContextByAnnotation(Class annotationConfig)

    {

        AnnotationConfigApplicationContext ApplicationContext = null;
        if (annotationConfig != null) {
            Configuration annotation = (Configuration) annotationConfig.getAnnotation(Configuration.class);
            if (annotation != null) {
                ApplicationContext = new AnnotationConfigApplicationContext(annotationConfig);
                log.info("This ApplicationContext 加载成功 getApplicationContextByAnnotation ");
            } else {
                log.info("This Class inInstance 没有包含@" + Configuration.class.getSimpleName());

            }
        }
        return ApplicationContext;
    }

    /**
     * @param XmlClassPath Xml文件Classpath路径 classpath:applicationContext.xml
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContextByClassPathXml(String XmlClassPath) {
        ApplicationContext ApplicationContext = null;
        if (XmlClassPath != null) {

            ApplicationContext = new ClassPathXmlApplicationContext(XmlClassPath);
            log.info("This ApplicationContext 加载成功 getApplicationContextByClassPathXml ");

        }

        return ApplicationContext;

    }

    /**
     * @param XmlPath xml文件的路径 可以是classpath 也可以是文件路径
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContextByFileSystemXml(String XmlPath) {
        ApplicationContext ApplicationContext = null;

        if (XmlPath != null) {

            ApplicationContext = new FileSystemXmlApplicationContext(XmlPath);
            log.info("This ApplicationContext 加载成功 getApplicationContextByFileSystemXml ");
        }
        return ApplicationContext;
    }


}
